package fr.afcepf.ai85.gepeto.managedBean;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.event.AjaxBehaviorEvent;
import javax.faces.model.SelectItem;
import fr.afcepf.ai85.ent.Eleve;
import fr.afcepf.ai85.ent.MessageEnvoye;
import fr.afcepf.ai85.gepeto.service.ServiceEleveHib;
import fr.afcepf.ai85.gepeto.service.ServiceMessageEnvoye;

@ManagedBean(name = "beanMessageEnvoye")
@SessionScoped
public class BeanMessageEnvoye {

	private Integer idMessageEnvoye;
	private List<SelectItem> listeMessageEnvoye = new ArrayList<SelectItem>();
	private Eleve el;
	private Integer idEd;
	private String message;
	private String objetMessageCreer;
	private String messageCreer;
	
	@ManagedProperty(value = "#{mbConnexion.leLogin}")
	private String nomLoginConnexion;
	
	private ServiceEleveHib seh;
	
	@PostConstruct
	public void init()
	{
//		System.out.println(nomLoginConnexion);
		seh=new ServiceEleveHib();
		el = seh.rechercherEleveByNomConnexion(nomLoginConnexion);
		idEd=el.getIdEleve();
	}
	
	public String getNomLoginConnexion() {
		return nomLoginConnexion;
	}


	public String getObjetMessageCreer() {
		return objetMessageCreer;
	}

	public void setObjetMessageCreer(String objetMessageCreer) {
		this.objetMessageCreer = objetMessageCreer;
	}

	public String getMessageCreer() {
		return messageCreer;
	}

	public void setMessageCreer(String messageCreer) {
		this.messageCreer = messageCreer;
	}

	public void setNomLoginConnexion(String nomLoginConnexion) {
		this.nomLoginConnexion = nomLoginConnexion;
	}


	public Integer getIdMessageEnvoye() {
		return idMessageEnvoye;
	}


	public void setIdMessageEnvoye(Integer idMessageEnvoye) {
		this.idMessageEnvoye = idMessageEnvoye;
	}


	public BeanMessageEnvoye() {
		
	}	


	public List<SelectItem> getListeMessageEnvoye() {
		listeMessageEnvoye = new ArrayList<SelectItem>();
		
		rechercheMessageEnvoye(idEd);
		return listeMessageEnvoye;
	}
	public void setListeMessageEnvoye(List<SelectItem> listeMessageEnvoye) {
		this.listeMessageEnvoye = listeMessageEnvoye;
	}

	public void rechercheMessageEnvoye(Integer idEleve){
				
		ServiceMessageEnvoye sme= new ServiceMessageEnvoye();
		List<MessageEnvoye> laListeM=sme.listeMessageEnvoye(idEleve);
		
		for(MessageEnvoye me:laListeM){
			SelectItem si = new SelectItem(me.getIdMessageEnvoye(), me.getObjetMessageEnv());
			listeMessageEnvoye.add(si);
			System.out.println(me.getObjetMessageEnv());
		}		
//		idMessageEnvoye = Integer.parseInt(listeMessageEnvoye.get(0).getValue().toString());
//		chargementMessageById();
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	public void recupMessage(AjaxBehaviorEvent event) {				
		System.out.println("Dans la methode recupMessage");
		chargementMessageById();
	}

	private void chargementMessageById() {
		ServiceMessageEnvoye sme= new ServiceMessageEnvoye();
		System.out.println("BeanMessage chargementMessageById idMessageEnvoye "+ idMessageEnvoye);
		message=sme.UnMessageEnvoye(idMessageEnvoye);
		
	}
	public String envoyerMessage(){
		ServiceMessageEnvoye sme= new ServiceMessageEnvoye();
		sme.creerMessage(idEd, objetMessageCreer, messageCreer);
		objetMessageCreer="";
		messageCreer="";
		return "";
	}
	
	
}
